﻿<!--#include virtual="/Common/Config.asp" -->
<!--#include virtual="/Common/conn.asp" -->
<!--#include virtual="/Common/Function.asp" -->
<!--#include virtual="/2010admin/Check_Priv.asp"-->

<%
PageTitle="T-SQL脚本执行器"

dim cmd,sql,OutErrorCode,PageNo,myPageSize

If  Request.ServerVariables ("Request_Method")<>"POST" Then
	ExecSqlType = "1"
Else
	
	SqlText=""
	if CStr(Session("Admin_SuperUser"))="1" then
	

		'***********************************************
		SqlText=SqlText & " --增列" & CR 
		SqlText=SqlText & " ALTER TABLE zxchem_ ADD URL varchar(120) null " & CR 
		SqlText=SqlText & " " & CR   & CR 

		SqlText=SqlText & " --改列宽 " & CR 
		SqlText=SqlText & " ALTER TABLE zxchem_ " & CR 
		SqlText=SqlText & " ALTER COLUMN contract_no varchar (80) NULL " & CR 
		SqlText=SqlText & " " & CR   & CR 

		SqlText=SqlText & " --重命名列 , 下例将表 dic_customers 中的列 OOLD_Col 重命名为 new_Col " & CR 
		SqlText=SqlText & " EXEC sp_rename 'dic_customers.[OOLD_Col]', 'new_Col', 'COLUMN' " & CR 
		SqlText=SqlText & " " & CR   & CR 

		SqlText=SqlText & " --删除约束、列 " & CR 
		SqlText=SqlText & " ALTER TABLE 表名 DROP CONSTRAINT 约束名 " & CR 
		SqlText=SqlText & " ALTER TABLE 表名 DROP COLUMN 列名 " & CR 
		SqlText=SqlText & " " & CR   & CR 


		SqlText=SqlText & " " & CR   & CR 
		'***********************************************

		SqlText=SqlText & " " & CR 
		SqlText=SqlText & " " & CR   & CR 
		ExecSqlType="0"

	Else
		SqlText=SqlText & " " & CR 
		SqlText=SqlText & " " & CR   & CR 
		ExecSqlType="1"

	end if

	ExecSqlType = reform("ExecSqlType")

End IF
myPageSize=20

%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="/2010admin/css/default.css" type="text/css" rel="stylesheet">
<title><%=web_title%></title>
</HEAD>


<script language="JavaScript">
<!--
function checkform() {

	var frm = document.form1;	


	var str=frm.SqlText.value;
	if (str==""){
		alert("[T-SQL脚本内容]不能为空!");
		return false;
	}



	var result;
	result=window.confirm("确定吗？")
	if (result==0) {
		return false;
	}


}
//-->
</script>

<BODY bgcolor="#FFFFFF">
<table border="0" width="100%" cellSpacing="0" cellPadding="0">
  <tr>
    <td width="100%" height="5"></td>
  </tr>
  <tr>
    <td width="100%" align="center">
      <table border="0" width="98%" cellSpacing="0" cellPadding="0">
        <tr>
          <td width="*" align="center" valign="top">
            <table border="0" width="100%" cellSpacing="0" cellPadding="0" class="lrbtline">
              <tr>
                <td width="100%" height="25" class="tbtop">管理导航</td>
              </tr>
              <tr>
                <td width="100%" align="center">
                  <table border="0" width="98%" cellSpacing="0" cellPadding="0">
                    <tr>
                      <td width="100%" height="5"></td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
            <table border="0" width="100%" cellSpacing="0" cellPadding="0">
              <tr>
                <td width="100%" height="5"></td>
              </tr>
            </table>
            <table border="0" width="100%" cellSpacing="0" cellPadding="0" class="lrbtline">
              <tr>
                <td width="100%" height="25" class="tbtop">T-SQL脚本执行器</td>
              </tr>
              <tr>
                <td width="100%" align="center">
                  <table border="0" width="98%" cellSpacing="0" cellPadding="0">
                    <tr>
                      <td width="100%" height="5"></td>
                    </tr>
                  </table>
                  <table border="0" width="98%" cellSpacing="0" cellPadding="0">
                  <form action="?action=add" method="post" name="form1" onSubmit="return checkform();">

                    <tr>
                      <td width="137" height="25"><div align="center"></div>
                        脚本内容</td>
                      <td><textarea name="SqlText" cols="80" rows="20"><%=reform("SqlText")%></textarea></td>
                    </tr>
                    <tr>
                      <td height="25">项目内容</td>
                      <td><%if CStr(Session("Admin_SuperUser"))="1" then%>
						  <input type="radio" name="ExecSqlType" value="0" <%=GetChecked("0",ExecSqlType)%> >
						  执行操作
						<%end if%>

						  <input type="radio" name="ExecSqlType" value="1" <%=GetChecked("1",ExecSqlType)%>>
						  显示数据-->显示前<input name="myPageSize" type="text" id="myPageSize" value="<%=myPageSize%>" size="3">条记录&nbsp;&nbsp;

					  </td>
                    </tr>
                    <tr>
                      <td height="25" >
						&nbsp;
					  </td>
					 <td> <input type="submit" name="submit1" value="提交" class="button">
					  </td>
                      </tr>
                  </form>
                  </table>

                </td>
              </tr>
            </table>

			<!--  -->
			<%
			If  Request.ServerVariables ("Request_Method")="POST" Then

			SqlText = trim(request.form("SqlText"))
			ExecSqlType = reform("ExecSqlType")
			myPageSize =reform("myPageSize")

			if Session("Admin_UserName")<>"admin" then
				SqlText=UCase(SqlText)

				If instr(SqlText,"INSERT")>0 OR instr(SqlText,"UPDATE")>0 OR instr(SqlText,"DELETE")>0 OR instr(SqlText,"DROP")>0 OR instr(SqlText,"ALTER")>0 OR instr(SqlText,"CREATE")>0 OR instr(SqlText,"EXEC")>0 OR instr(SqlText,"SP_")>0 Then 
						AlertBox  "只能进行查询操作！", "javascript:history.go(-1)"
				end if
			end if


			If reform("ExecSqlType")="1" Then '获取数据显示在页面上
				'call ShowData() 
			%><BR>

            <table border="0" width="100%" cellSpacing="0" cellPadding="0" class="lrbtline">
              <tr>
                <td width="100%" height="25" class="tbtop">结果</td>
              </tr>
              <tr>
                <td width="100%" align="center">
				<% 

				Call ShowData()
				If  Request.ServerVariables ("Request_Method")="POST" AND reform("ExecSqlType")="1" Then
						
						If PageNo<=0 Or PageNo="" Then PageNo=1


						
						Call ShowOnePage(rs,PageNo,Sql,myPageSize)

						response.write "<BR><input type='button' class=inputbutton name='back' value='返回' onclick='javascript:history.go(-1)'><BR><BR> "

				End If

				%>


                </td>
              </tr>
            </table>
			<!--  -->
			<% Else 
				call change() '对数据库进行操作
			End If
			End If 
			%>

                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td width="100%" height="5"></td>
  </tr>
</table>


</div>
</form>

<% Conn_Close() %>
</BODY> 
</HTML> 



	
<%
SUB change()

	on error resume next
	dim sql,cmd

	set cmd = server.CreateObjecT("ADODB.COMMAND")
	Sql = request.form("SqlText")

   ' response.write sql

	cmd.ActiveConnection  = conn
	cmd.CommandType = 4
	cmd.CommandText = "ExecuteSql"
	cmd.Parameters(1) = sql

	cmd.Execute 

	OutErrorCode =cmd.parameters(2)
	
	

	Set cmd = Nothing
	conn.close
	'Set conn = Nothing
	

	if Err.Number=0 AND OutErrorCode=0 then

		AlertBox  "脚本执行成功！", "javascript:history.go(-1)" ' "Execute_SQL.asp?db="&DB

	else
		writebr "脚本执行失败！"
		writebr Err.Description
		'AlertBox  "脚本执行失败！", "javascript:history.go(-1)"

	end If

	
END SUB



Sub  ShowData()

	On Error Resume Next
	dim sql	
	Sql = request.form("SqlText")
	rs.open sql,conn,1,3
	
	if rs.eof and rs.bof then
		AlertBox  "没有查询到记录！", "javascript:history.go(-1)"
	end if

	myPageSize =reform("myPageSize")
	
	If ISNUMERIC(myPageSize)=False Then
		myPageSize = 10
	End If
	
	if Err.Number<>0 Then
		AlertBox  "SQL语句错误，请检查！", "javascript:history.go(-1)"
	end If

	
End Sub 



Sub ShowOnePage(rs,Page,Sql,myPageSize)

  Response.Write "<CENTER><table border='1' cellpadding='2' width='100%' cellspacing='0'>"
  Response.Write "<TR BGCOLOR=#CCCCCC>"
  Response.WRITE "<TD>No.</TD>"
     for i=0 to rs.fields.count-1
       Response.WRITE "<TD><small>" & rs.Fields(i).Name & "</small></TD>"
     next
  Response.Write "</TR>"

  
  rs.PageSize = myPageSize
  rs.AbsolutePage = Page
  For iPage = 1 To rs.PageSize
     Response.Write "<TR>"
     RecNo = (Page - 1) * rs.PageSize + iPage
     Response.Write "<TD><small>" & RecNo & "</small></TD>"

     for i=0 to rs.fields.count-1
       if rs.Fields(i).value <> "" then
          Response.WRITE "<TD><small><p align='right'>" & rs.Fields(i).Value & " </small></TD>"
       else
          Response.WRITE "<TD><small><p align='right'>--</TD>"
       end if
     next

     Response.Write "</TR>"
     rs.MoveNext
     If rs.EOF Then Exit For
  Next
  Response.Write "</TABLE></CENTER>"  

End Sub

%>